<?php
namespace app\controllers;
use yii;
use yii\web\Controller;


/**
 * 控制器基类
 */
class BaseController extends Controller
{

	public $layout = false;
	public $data = ['theme' => '/assets/', 'breadcrumbs' => ''];
	public $uid = 0;
    public $params;
    public $return = array(
        'status' => 1,
        'error' => array('errorno' => 0, 'errormsg' => 'success'),
        'data' => array()
    );

	public function init()
    {
        $this->enableCsrfValidation = false;
        $get_params = Yii::$app->request->get();
        $post_params = Yii::$app->request->post();
        $params = array_merge($get_params, $post_params);
        $this->params = $params;
        parent::init(); // TODO: Change the autogenerated stub
    }

    public function getQueryparams($name, $defaultValue, $data_type = '')
    {
        $returnvalue = isset($this->params[$name]) ? $this->params[$name] : $defaultValue;
        if (isset($this->params[$name]) && $data_type != '') {
            switch ($data_type) {
                case 'int':
                    //echo $this->params[$name];die;
                    //var_dump(filter_var($this->params[$name],FILTER_VALIDATE_INT));die;
                    if (filter_var($this->params[$name], FILTER_VALIDATE_INT) || filter_var($this->params[$name], FILTER_VALIDATE_INT) === 0) {
                        $returnvalue = (int)$this->params[$name];
                    } else {
                        $returnvalue = $defaultValue;
                    }
                    break;
                case 'float':
                    if (filter_var($this->params[$name], FILTER_VALIDATE_FLOAT) || filter_var($this->params[$name], FILTER_VALIDATE_FLOAT) === 0) {
                        $returnvalue = (float)$this->params[$name];
                    } else {
                        $returnvalue = $defaultValue;
                    }
                    //$returnvalue= filter_var($this->params[$name],FILTER_VALIDATE_FLOAT)?(float)$this->params[$name]:$defaultValue;
                    break;
                default:
                    $returnvalue = is_string($this->params[$name]) ? $this->params[$name] : $defaultValue;
                    break;
            }
        }

        return $returnvalue;
    }

    /**
     *  返回成功json数据
     * @param null $data
     */
    public function response($data = null)
    {
        if (empty($data)) {
            $this->return['data'] = [];
        } else {
            $this->return['data'] = $data;
        }
        $format_data = $this->formatResponse($this->return);
        echo json_encode($format_data);
        exit;

//        $json = json_encode($this->data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
//        echo str_replace(':null', ':""', $json);
//        Yii::info('api_url['.Yii::$app->request->url.'] '.PHP_EOL.' run time:'.(microtime(true)-$this->startime.' '.microtime(true).'='.$this->startime),'apiruntime');
//        exit;

    }

    public function formatResponse($data)
    {
        if(is_array($data) || is_object($data) ){
            $arr = [];
            foreach($data as $key=>$item)
            {
                if(is_array($item) || is_object($item)){

                    $arr[$key] = $this->formatResponse($item);
                }else{
                    $item = str_replace(':null', ':""', $item);
                    $arr[$key] = $item;
                }
            }
            return $arr;
        }else{
            return $data;
        }
    }

    /**
     * 返回错误信息
     * @param string $no 状态码
     * @param string $msg 错误信息
     */
    protected function error($no, $msg, $data = null)
    {
        $this->return['error'] = array('errorno' => $no, 'errormsg' => $msg,);
        $this->return['status'] = 0;
        $this->response($data);
    }

    public function render ($tpl, $data = [])
    {
		return parent::render($tpl . '.html', $data);
	}

	public function renderPartial ($tpl, $data = [])
	{
		return parent::renderPartial($tpl . '.html', $data);
	}


}